package Controller;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFileChooser;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import Model.PlayerModel;
import View.PlayerView;


public class PlayerController implements ActionListener, ChangeListener, KeyListener {
	
	@SuppressWarnings("unused")
	private static final Component parent = null;
	private static PlayerController instance;
	public static JFileChooser chooser;
	static String userhome1 = System.getProperty("user.home");
	
	public static PlayerController getInstance() {
		if (instance == null) {
			instance = new PlayerController();
		}
		return instance;
	}
	
	public PlayerController(){
		View.PlayerView.btnPlay.setActionCommand("play");
		View.PlayerView.btnStop.setActionCommand("stop");
		View.PlayerView.btnBrowse.setActionCommand("browse");
		View.PlayerView.btnForward.setActionCommand("next");
		View.PlayerView.btnBackward.setActionCommand("prev");
		View.PlayerView.btnDeleteSongFormPlaylist.setActionCommand("dfp");
		
		View.PlayerView.btnAddCurrentPlaylist.setActionCommand("addPL");
		View.PlayerView.btnRemoveCurrentPlaylist.setActionCommand("removePL");
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if("play".equals(e.getActionCommand())){
			Model.PlayerModel.getInstance().playPause();
		}
		
		if("stop".equals(e.getActionCommand())){
			Model.PlayerModel.getInstance().stopPressed();
		}
		
		if("dfp".equals(e.getActionCommand())){
			PlayerModel.getInstance().removeSong();
		}
		
		if("browse".equals(e.getActionCommand())){
			Model.PlayerModel.getInstance().browse();
		}
		
		if("next".equals(e.getActionCommand())){
			Model.PlayerModel.getInstance().next();	
		}
		
		if("prev".equals(e.getActionCommand())){
			Model.PlayerModel.getInstance().prev();		
		}
		
		if("addPL".equals(e.getActionCommand())){
			Model.CurrentPlayList.getInstance().addSongs();
		}
		
		if("removePL".equals(e.getActionCommand())){
			Model.CurrentPlayList.getInstance().removeSongs();				
		}		
	}
	

	@Override
	public void stateChanged(ChangeEvent e) {
//		PlayerModel.getInstance().editTimeline();
		PlayerModel.getInstance().setVolume();
	}

	@Override
	public void keyPressed(KeyEvent e) {
		
		if(PlayerView.search.isFocusOwner()){
			PlayerModel.getInstance().search(PlayerView.search.getText());
		}
	
		if(e.getKeyCode() == KeyEvent.VK_DELETE){
			PlayerModel.getInstance().removeSong();
		}
	}

	@Override
	public void keyReleased(KeyEvent e) {

	}

	@Override
	public void keyTyped(KeyEvent e) {

	}

}
